﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class YP_LJTZ : Form
    {
        public static string currow;
        public static int state_flag;
        DataTable dt = new DataTable();
        public YP_LJTZ()
        {
            InitializeComponent();
        }

        private void edit()
        {
            SqlConnection conn = new SqlConnection(SQL.connectDB);
            conn.Open();

            SqlCommand sc_f = new SqlCommand("SELECT FLAG FROM YP_LJTZCZ WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", conn);//sql语句
            if (Convert.ToInt16(sc_f.ExecuteScalar()) == 2)
                state_flag = 2;
            if (Convert.ToInt16(sc_f.ExecuteScalar()) == 1)
                state_flag = 1;
            //if (Convert.ToInt16(sc_f.ExecuteScalar()) == 2)
            //    MessageBox.Show("该冲账单号已记账,不能更改!", "提示");
            //if (Convert.ToInt16(sc_f.ExecuteScalar()) == 1)
            //    MessageBox.Show("该冲账单号已审核,请先消审!", "提示");
            //if (Convert.ToInt16(sc_f.ExecuteScalar()) == 0)
            //{
            if (Convert.ToInt16(sc_f.ExecuteScalar()) == 0)
                state_flag = 0;
            currow = dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString();
            YP_LJTZCZD ljtzczd = new YP_LJTZCZD();
            ljtzczd.Show();
            currow = null;
            //}
        }

        private void change_color(int i)
        {
            if (i == 0)
                dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Black;
            if (i == 1)
                dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Blue;

            if (i == 2)
                dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Green;

        }

        private void state_color(int i)
        {
            if (Convert.ToInt16(dataGridViewX1.Rows[i].Cells[6].Value) == 1)
                dataGridViewX1.Rows[i].DefaultCellStyle.ForeColor = System.Drawing.Color.Blue;

            if (Convert.ToInt16(dataGridViewX1.Rows[i].Cells[6].Value) == 2)
                dataGridViewX1.Rows[i].DefaultCellStyle.ForeColor = System.Drawing.Color.Green;

        }

        private void btn_refresh_Click(object sender, EventArgs e)
        {
            string str_flag = "";
            if (cb_wsh.Checked == true)//未审
            {
                if (cb_ysh.Checked == true || cb_yjz.Checked == true)
                    str_flag = str_flag + " AND FLAG=0";
                else str_flag = " AND FLAG=0";
            }
            if (cb_ysh.Checked == true)//已审
            {
                if (cb_wsh.Checked == false && cb_yjz.Checked == true)
                    str_flag = str_flag + " AND FLAG=1";
                if (cb_wsh.Checked == true)
                    str_flag = str_flag + " OR FLAG=1";
                else str_flag = " AND FLAG=1";
            }
            if (cb_yjz.Checked == true)//记账
            {
                if (cb_wsh.Checked == true || cb_ysh.Checked == true)
                    str_flag = str_flag + " OR FLAG=2";
                else str_flag = " AND FLAG=2";
            }
            if (cb_wsh.Checked == false && cb_ysh.Checked == false && cb_yjz.Checked == false)
                str_flag = " AND FLAG IS NULL";
            try
            {
                dt.Clear();
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    SqlDataAdapter da = new SqlDataAdapter();//实例化sqldataadpte
                    SqlCommand cmd1 = new SqlCommand("SELECT * FROM YP_LJTZCZ WHERE RQ BETWEEN '" + dateTimeInput1.Value + "' AND '" + dateTimeInput2.Value + "' AND(1=1 " + str_flag + ")", con);//sql语句
                    da.SelectCommand = cmd1;//设置为已实例化SqlDataAdapter的查询命令
                    da.Fill(dt);
                    dataGridViewX1.DataSource = dt;

                    dataGridViewX1.Columns[0].HeaderText = "调价单号";
                    dataGridViewX1.Columns[1].HeaderText = "录入日期";
                    dataGridViewX1.Columns[2].HeaderText = "录入人员";
                    dataGridViewX1.Columns[3].HeaderText = "审核人员";
                    dataGridViewX1.Columns[4].HeaderText = "记账人员";
                    dataGridViewX1.Columns[5].HeaderText = "记账日期";
                    dataGridViewX1.Columns[6].Visible = false;
                    dataGridViewX1.Columns[7].Visible = false;

                    for (int i = 0; i < dataGridViewX1.RowCount; i++)
                        state_color(i);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con_sc = new SqlConnection(SQL.connectDB);
                con_sc.Open();

                SqlCommand sc_f = new SqlCommand("SELECT FLAG FROM YP_LJTZCZ WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_sc);//sql语句

                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 2)
                    MessageBox.Show("该冲账单号已记账,不能删除!", "提示");

                else
                {
                    SqlCommand del = new SqlCommand("DELETE YP_LJTZCZ WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_sc);//sql语句
                    del.ExecuteNonQuery();
                    dt.Rows.RemoveAt(dataGridViewX1.CurrentCell.RowIndex);
                    MessageBox.Show("删除成功！");
                    dataGridViewX1.DataSource = dt;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        
       
        }

        private void dataGridViewX1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            edit();
        }

        private void btn_new_Click(object sender, EventArgs e)
        {

        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            edit();
        }

        private void YP_LJTZ_Load(object sender, EventArgs e)
        {
            dateTimeInput1.Value = DateTime.Today;
            dateTimeInput2.Value = DateTime.Today;
            cb_wsh.Checked = true;
            cb_ysh.Checked = true;
            cb_yjz.Checked = true;
        }

        private void btn_sh_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con_sh = new SqlConnection(SQL.connectDB);
                con_sh.Open();

                SqlCommand sc_f = new SqlCommand("SELECT FLAG FROM YP_LJTZCZ WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_sh);//sql语句

                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 2)
                    MessageBox.Show("该冲账单号已记账,不能更改!", "提示");
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 1)
                    MessageBox.Show("该冲账单号已审核!", "提示");
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 0)
                {
                    SqlCommand sc_sh = new SqlCommand("UPDATE YP_LJTZCZ SET FLAG=1 WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_sh);//sql语句
                    sc_sh.ExecuteNonQuery();
                    change_color(1);
                    MessageBox.Show("审核成功！");
                    dataGridViewX1.DataSource = dt;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btn_zx_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con_sh = new SqlConnection(SQL.connectDB);
                con_sh.Open();

                SqlCommand sc_f = new SqlCommand("SELECT FLAG FROM YP_LJTZCZ WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_sh);//sql语句
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 0)
                    MessageBox.Show("请先审核!", "提示");
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 2)
                    MessageBox.Show("该冲账单号已经记账!", "提示");
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 1)
                {
                    SqlCommand sc_jz = new SqlCommand("UPDATE YP_LJTZCZ SET FLAG=2 WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_sh);//sql语句
                    sc_jz.ExecuteNonQuery();
                    change_color(2);
                    MessageBox.Show("记账成功！");
                    dataGridViewX1.DataSource = dt;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btn_xs_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con_xs = new SqlConnection(SQL.connectDB);
                con_xs.Open();

                SqlCommand sc_f = new SqlCommand("SELECT FLAG FROM YP_LJTZCZ WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_xs);//sql语句

                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 2)
                    MessageBox.Show("该冲账单号已记账,不能更改!", "提示");
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 0)
                    MessageBox.Show("该冲账单号还没审核!", "提示");
                if (Convert.ToInt16(sc_f.ExecuteScalar()) == 1)
                {
                    SqlCommand sc_xs = new SqlCommand("UPDATE YP_LJTZCZ SET FLAG=0 WHERE TJDH='" + dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'", con_xs);//sql语句
                    sc_xs.ExecuteNonQuery();
                    change_color(0);
                    MessageBox.Show("消审成功！");
                    dataGridViewX1.DataSource = dt;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btn_new_Click_1(object sender, EventArgs e)
        {
            YP_LJTZD LJTZD = new YP_LJTZD();
            LJTZD.Show();
        }


    }
}